package simpledb.log;

import static simpledb.file.Page.*;
import simpledb.file.Page;

/**
 * A class that provides the ability to read the values of a log record. The
 * class has no idea what values are there. Instead, the methods
 * {@link #nextInt() nextInt} and {@link #nextString() nextString} read the
 * values sequentially. Thus the client is responsible for knowing how many
 * values are in the log record, and what their types are.<br>
 * 提供读取日志记录值的能力的类。 类不知道有什么值。 相反，方法{@link #nextInt() nextInt}和
 * {@link #nextString() nextString}依次读取值。 因此，客户端负责知道日志记录中有多少值，以及它们的类型。
 * 
 * @author Edward Sciore
 */
public class BasicLogRecord {
  private Page pg;// 包含日志记录的页面
  private int pos;// 日志记录的位置

  /**
   * A log record located at the specified position of the specified page. This
   * constructor is called exclusively by {@link LogIterator#next()}.<br>
   * 位于指定页面的指定位置的日志记录。 此构造函数由{@link LogIterator#next()}独占调用。
   * 
   * @param pg the page containing the log record 包含日志记录的页面
   * @param pos the position of the log record 日志记录的位置
   */
  public BasicLogRecord(Page pg, int pos) {
    this.pg = pg;
    this.pos = pos;
  }

  /**
   * Returns the next value of the current log record, assuming it is an
   * integer.<br>
   * 返回当前日志记录的下一个值，假设它是一个整数。
   * 
   * @return the next value of the current log record
   */
  public int nextInt() {
    int result = pg.getInt(pos);
    pos += INT_SIZE;
    return result;
  }

  /**
   * Returns the next value of the current log record, assuming it is a string.<br>
   * 返回当前日志记录的下一个值，假设它是一个字符串。
   * 
   * @return the next value of the current log record
   */
  public String nextString() {
    String result = pg.getString(pos);
    pos += STR_SIZE(result.length());
    return result;
  }
}
